/*
Rilego - An easy Images 2 E-Book converter.
Copyright (C) 2011  Federico Improta (Kirys)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package it.kirys.rilego.engine.processors.transformers;

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

/**
 * This class split an image in two images, this strategy is based on the
 * original JEComics code
 * @author kirys
 */
public class SplitToTwo implements IImageSplitter {

	boolean mangaMode;

	public boolean isMangaMode() {
		return mangaMode;
	}

	public void setMangaMode(boolean mangaMode) {
		this.mangaMode = mangaMode;
	}

	public SplitToTwo(boolean mangaMode) {
		this.mangaMode = mangaMode;
	}

	public SplitToTwo() {
		this.mangaMode = false;
	}

	/**
	 * Get half of the image
	 * @param source image to extract the halve from
	 * @param leftHalve true return the halve from the left false return the halve from the right
	 * @return an halve of the image
	 */
	public static BufferedImage getHalf(BufferedImage source, boolean leftHalve) {
		int w = source.getWidth() / 2;
		int h = source.getHeight();
		int x = (leftHalve) ? 0 : w;
		BufferedImage out = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
		Graphics2D g = out.createGraphics();
		//white back
		g.setColor(new java.awt.Color(0xff, 0xff, 0xff));
		g.draw(new Rectangle(0, 0, w, h));
		g.fill(new Rectangle(0, 0, w, h));
		//now we have to take half of the image starting from x and ending from x+w
		g.drawImage(source, 0, 0, w, h, x, 0, x + w, h, null);
		g.dispose();
		return out;
	}//public static BufferedImage getHalf(BufferedImage source, boolean leftHalve)

	@Override
	public BufferedImage[] split(BufferedImage source) {
		BufferedImage A = getHalf(source, !isMangaMode());
		BufferedImage B = getHalf(source, isMangaMode());
		return new BufferedImage[]{A, B};
	}
}
